import axios from "axios";
import VueLoader from "./loading.vue";

export default {
  install(Vue, options) {
    Vue.prototype.__loader_checks = [];
    Vue.prototype.$__loadingHTTP = new Proxy(
      {},
      {
        set: function(target, key, value, receiver) {
          let oldValue = target[key];
          if (!oldValue) {
            Vue.prototype.__loader_checks.forEach(func => {
              func(key, value);
            });
          }

          return Reflect.set(target, key, value, receiver);
        }
      }
    );

    axios.interceptors.request.use(config => {
      Vue.prototype.$__loadingHTTP[config.url] = config;

      return config;
    });

    axios.interceptors.response.use(response => {
      delete Vue.prototype.$__loadingHTTP[response.config.url];

      return response;
    });
    Vue.mixin({
      beforeCreate() {
        Vue.component("v-loader", VueLoader);
      }
    });
  }
};
